<?php

namespace App\Http\Controllers\Develop;

use Carbon\Carbon;
use GuzzleHttp\Client;
use Illuminate\Http\Request;
use App\Models\User\Access;
use App\Models\ConfigureFunction;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Redis as Redis;

class AccessController extends Controller
{
    public function index()
    {
        $results = Access::select('access_token')
            ->orderBy('timeStamp', 'desc')
            ->first();

        return view ('develop/accessList',$results);
    }

    //access_token列表
    public function search(Request $request)
    {
        $where = [];

        $access_token = $request->input('access_token');

        if (!empty($access_token)) {
            $where[] = ['access_token',$access_token];
        }

        $log_data = Access::where($where)
            ->orderBy($request->input('sort'), $request->input('order'))
            ->paginate($request->input('limit'))
            ->toArray();

        //返回数组
        $return = [
            'total' => $log_data['total'],
            'rows' => []
        ];

        if ( $log_data['data'] ) {

            foreach( $log_data['data'] as $row ){

                if ( $row['get_type'] == 1){
                    $get_type = '到期自动获取';
                }else{
                    $get_type = '重置获取';
                }
                $return['rows'][] = [
                    'creator' => $row['creator'],
                    'createTime' => $row['createTime'],
                    'get_type' => $get_type,
                    'access_token' => $row['access_token']
                ];

            }

        }

        return response()->json($return);

    }


    //重置access_token
    public function reset(){

        $wx_setting_data = ConfigureFunction::find(41);
        $wx_setting_data = json_decode($wx_setting_data->configure_json,true);
        if( !isset($wx_setting_data['configure']) ){
            return null;
        }
        $wx_setting_data = $wx_setting_data['configure'];
        //设置token存储的redis索引
        $redis_token_index = 'WECHAT_ACCESS_TOKEN_'.$wx_setting_data['wechat_originalid'];

        //http实例
        $client = new Client(['verify'=>false]);

        //请求链接
        $http_url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=' . $wx_setting_data['wechat_appid'] . '&secret=' . $wx_setting_data['wechat_appsecret'];

        $response = $client->get($http_url);
        $code = $response->getStatusCode();
        if ( $code != 200 ) {
            return null;
        }
        $return_data = json_decode($response->getBody()->getContents(), true);

        $access_token = '';

        if (isset($return_data['access_token']) && !empty($return_data['access_token'])) {

            $access_token = $return_data['access_token'];

            //记录获取access_token日志

            app('db')->table('log_wechat_get_access_token')->insertGetId([
                'uuid' => makeUuid(),
                'timeStamp' => Carbon::now(),
                'creator' =>  Redis::get('WDH_ADMIN_USER_ID_' . session_id()),
                'createTime' => Carbon::now(),
                'access_token' => $access_token,
                'get_type' => 2,
            ]);

            $tmp = [
                'SC_TIMESTAMP' => time(),
                'SC_TIMEOUT' => 7000,
                'SC_VALUE' => $access_token
            ];
            Redis::setex($redis_token_index, 7000, json_encode($tmp));
        }

        return array( 'code' =>200, 'message' => '重置成功', 'data' => $access_token);

    }

}